package com.hyyt.shopjsp.wxmg.util;

import it.sauronsoftware.jave.*;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * 音频文件转换器，amr格式转换为mp3格式
 */
public class WxConvertMP3 {
	/**
	 * 日志
	 */
	public static Logger logger = Logger.getLogger(WxConvertMP3.class);

  /**
   * 转换格式
   * @param sourcePath 源文件路径加文件名
   * @param targetPath 目标文件路径加文件名
   */
    public static void changeToMp3(String sourcePath, String targetPath) {  
        File source = new File(sourcePath);  
        File target = new File(targetPath);  
        AudioAttributes audio = new AudioAttributes();  
        Encoder encoder = new Encoder();  
  
        audio.setCodec("libmp3lame");  
        EncodingAttributes attrs = new EncodingAttributes();  
        attrs.setFormat("mp3");  
        attrs.setAudioAttributes(audio);  
  
        try {  
            encoder.encode(source, target, attrs);  
        } catch (IllegalArgumentException e) {
            logger.error(e);
        } catch (InputFormatException e) {
            logger.error(e);
        } catch (EncoderException e) {
            logger.error(e);
        }  
    }  
}
